package com.example.spring_task.controller;
import java.util.concurrent.Future;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.spring_task.async.AsyncTest;
import com.example.spring_task.utils.JsonData;
@RestController
@RequestMapping("/api/v1")
public class AsyncController {
​
    /**
     *  将异步类注解进来
     */
    @Autowired
    private AsyncTest asyncTest;
    
    @GetMapping("asyncTest")
    public JsonData exetask() throws InterruptedException {
        long begin=System.currentTimeMillis();
//      asyncTest.task1();
//      asyncTest.task2();
//      asyncTest.task3();
        Future<String> task4=asyncTest.task4();
        Future<String> task5=asyncTest.task5();
        Future<String> task6=asyncTest.task6();
        for (;;) {
            if(task4.isDone()&&task5.isDone()&&task6.isDone()) {
                break;
            }
        }
        
        long end=System.currentTimeMillis();
        long total=end-begin;
        System.out.println("执行总耗时"+total);
        return JsonData.buildSuccess(total);
    }
}
​
